<?php
/*
 * Created on 17-Jan-2005
 *
 */

using('System.Data');

class DataModule extends TModule
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		//$this->Database->setDataSourceName($this->Application->getUserParameter('DSN'));
		require("config.php");
		$this->Database->setDataSourceName($dsn);
	}
	
	public function loadAccount($name='')
	{
		if(empty($name))
			$name=$this->User->getUsername();
		$result=$this->Database->Execute("SELECT * FROM user WHERE name='$username'");
		if($result->RecordCount()>0)
			return $result->fields;
		else
			return null;
	}

	public function createAccount($name, $password, $email, $admin, $repositorygrants)
	{
		require("config.php");				
		
		//Add user to svn password file
		//Escape special strings in htpasswd command
		$password = eregi_replace('"', '\"', $password); 
		if(!file_exists("$svn_passwd_file"))		
		{
			exec("$htpassword_cmd -cmb $svn_passwd_file $name \"$password\""); 
		} else {
			exec("$htpassword_cmd -bm $svn_passwd_file $name \"$password\""); 
		}
		
		$md5_pw = md5($password);
		return $this->Database->Execute("INSERT INTO users (id, name, password, email, admin, repositorygrants) VALUES (null, '$name', '$md5_pw', '$email', '$admin', '$repositorygrants')");
		//return $this->Database->Execute("INSERT INTO users (name, password, email, admin, repositorygrants) VALUES ('$name', MD5('$password'), '$email', '$admin', '$repositorygrants')");
	}

	public function updateAccount($id, $name, $email, $admin, $repositorygrants)
	{		
		return $this->Database->Execute("UPDATE users SET name='$name', email='$email', admin='$admin', repositorygrants='$repositorygrants' WHERE id='$id'");
	}
	
	public function updatePassword($id, $password)
	{
		require("config.php");		
		
		$results = $this->Database->Execute("SELECT name FROM users WHERE id='$id'");
		$name = $results->fields['name'];

		//Escape special strings in htpasswd command 
		$password = eregi_replace('"', '\"', $password); 
		exec("$htpassword_cmd -mb $svn_passwd_file $name \"$password\""); 
		
		$md5_pw = md5($password);
		$this->Database->Execute("UPDATE users SET password='$md5_pw' WHERE id='$id'");
		//$this->Database->Execute("UPDATE users SET password=MD5('$password') WHERE id='$id'");
		return;
	}

	public function isUsernameTaken($name)
	{
		$result=$this->Database->Execute("SELECT * FROM users WHERE name='$name'");
		return $result->RecordCount()>0;
	}
	
	public function isEmailTaken($email)
	{
		$result=$this->Database->Execute("SELECT * FROM users WHERE email='$email'");
		return $result->RecordCount()>0;	
	}
	
	public function isValidTicket($ticket)
	{
		$result=$this->Database->Execute("SELECT * FROM usertickets WHERE ticket='$ticket'");
		return $result->RecordCount()>0;
	}
	
	public function hasTicket($email)
	{
		$result=$this->Database->Execute("SELECT * FROM usertickets WHERE email='$email'");
		return $result->RecordCount()>0;
	}
	
	public function createTicket($email, $ticket, $repos)
	{
		$result = $this->Database->Execute("INSERT INTO usertickets (email, ticket, repositorygrants) VALUES ('$email', '$ticket', '$repos')");
	}
	
	public function getTicket($ticket)
	{
		$result = $this->Database->Execute("SELECT email, ticket, repositorygrants FROM usertickets WHERE ticket='$ticket'");
		return $result->fields;		
	}
	
	public function removeTicket($ticket)
	{
		$result = $this->Database->Execute("DELETE FROM usertickets WHERE ticket='$ticket'");		
	}	
	public function getUsername($id)
	{
		$user = $this->Database->Execute("SELECT * FROM users WHERE id='$id'");
		if($user)
			return $user->fields['name'];
		else
			return null;
	}

	public function rebuildAccessFile()
	{
		require_once("./svnmanager/library/class.accessfile.php");
		$accessfile = new AccessFile();
		$accessfile->createFromDatabase();
	}

 public function deleteUser($id) 
{ 
	require("config.php"); 
 
	//Change Groups 
	$this->Database->Execute("UPDATE groups SET adminid='1' WHERE adminid='$id'"); 
  
	//Change Repositories 
	$this->Database->Execute("UPDATE repositories SET ownerid='1' WHERE ownerid='$id'"); 
 
	//Remove user from group(s) 
	$this->Database->Execute("DELETE FROM usersgroups WHERE userid='$id'"); 
 
	//Remove user's privileges
	$this->Database->Execute("DELETE FROM userprivileges WHERE userid='$id'");
	
	//Klaus Drechsler, 12.08.2005: 
	//bugfix: select name was after the user has been deletetd 
	$results = $this->Database->Execute("SELECT name FROM users WHERE id='$id'"); 
	$name = $results->fields['name']; 
 
	//Remove user 
	$this->Database->Execute("DELETE FROM users WHERE id='$id'"); 
 
	//$results = $this->Database->Execute("SELECT name FROM users WHERE id='$id'"); 
	//$name = $results->fields['name']; 
 
	exec("$htpassword_cmd -D $svn_passwd_file $name"); 
 
	$this->rebuildAccessfile(); 
} 	
	public function checkPassword($userid, $password)
	{
		//Check if admin is config file defined administrator (id=0)
		if($userid=='0')
		{
			require("config.php");
			if($password==$admin_temp_password)
				return true;
			else 
				return false;
		}
		
		$md5_pw = md5($password);
		$result = $this->Module->Database->Execute("SELECT * FROM users WHERE id='$userid' AND password='$md5_pw'");
		return $result->RecordCount()>0;
	
	}
		
	
	
}

?>